package com.jxj.study.controller;

import com.jxj.study.util.SecurityUtil;
import com.jxj.study.util.XmlUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;


@Controller
@RequestMapping("/wx")
public class GongZongHao {


    @RequestMapping(method = RequestMethod.GET)
    public void pay(HttpServletRequest rqu, HttpServletResponse rsp) throws IOException {
        rqu.getParameter("");
        String token = "weixin";
        rqu.setCharacterEncoding("UTF-8");
        String signature = rqu.getParameter("signature");
        System.out.println(signature);
        String timestamp = rqu.getParameter("timestamp");
        System.out.println(timestamp);
        String nonce = rqu.getParameter("nonce");
        System.out.println(nonce);
        String echostr  = rqu.getParameter("echostr");
        System.out.println(echostr);
        String[] strArr = {token,timestamp,nonce};
        Arrays.sort(strArr);//排序
        StringBuilder sb = new StringBuilder();
        for (String str : strArr) {
            sb.append(str);
        }
        String s = SecurityUtil.sha1(sb.toString());
        System.out.println( "当前时间 ="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"这是  pay 接口");
        rsp.setCharacterEncoding("UTF-8");
        if (signature.equals(s)){
            rsp.getWriter().print(echostr);
        }
        //return  "当前时间 ="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"这是  pay 接口";
    }

    @RequestMapping(method = RequestMethod.POST)
    public void massage(HttpServletRequest rqu, HttpServletResponse rsp) throws IOException {
        ServletInputStream in = rqu.getInputStream();
        InputStreamReader reader = new InputStreamReader(in);
        BufferedReader buffReader = new BufferedReader(reader);
        Map<String, Object> parameter = XmlUtil.parameter(buffReader);
        /*String s = "";
        while ((s = buffReader.readLine())!=null){
            System.out.println(s);
        }*/
        parameter.entrySet().stream().forEach(System.out::println);
        System.out.println("ToUserName==="+parameter.get("FromUserName"));
        System.out.println("FromUserName==="+parameter.get("ToUserName"));
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        sb.append("<ToUserName><![CDATA["+parameter.get("FromUserName")+"]]></ToUserName>");
        sb.append("<FromUserName><![CDATA["+parameter.get("ToUserName")+"]]></FromUserName>");
        sb.append("<CreateTime>"+new Date().getTime()+"</CreateTime>");
        sb.append("<MsgType><![CDATA[text]]></MsgType>");
        sb.append("<Content><![CDATA[你好]]></Content>");
        sb.append("</xml>");
        //String massage = "<xml><ToUserName><![CDATA[gh_f46be312ac14]]></ToUserName><FromUserName><![CDATA[osZuswv1EnWmFAranp1g2mv_RsYc]]></FromUserName><CreateTime>"+System.currentTimeMillis()+"</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好1111]]></Content></xml>";
        rsp.getWriter().print(sb.toString());

    }
}
